﻿//------------------------------------------------------------------------------
// <copyright file="MetadataProcessorTest.cs" company="CommerceServer.net, Inc">
//    (c) 2012 CommerceServer.net, Inc. and its affiliates. All rights reserved.
// </copyright>
// <summary></summary>
//------------------------------------------------------------------------------ 
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CommerceServer.Foundation.Definitions;
using CommerceServer.Foundation;

namespace CommerceServerContrib.CodeGeneration.Test
{
    [TestClass]
    public class MetadataProcessorTest
    {
        [TestMethod]
        public void GetEntities()
        {
            var metadataProcessor = new CommerceServerMetadataProcessor();
            var entities = metadataProcessor.ClassDefinitions();
            Assert.IsNotNull(entities, "The entity collection is null");
            Assert.AreEqual(42, entities.Count);
        }

        [TestMethod]
        public void GetCompleteDefinition()
        {
            var metadataProcessor = new CommerceServerMetadataProcessor();
            var specificEntity = metadataProcessor.GetFullDefinition(null, "SiteContent");
            Assert.IsNotNull(specificEntity, "Entity is null");
            Assert.AreEqual(13, specificEntity.PropertyDefinitions.Count);
        }

        [TestMethod]
        public void GetClassDefinitionForSpecificDefinition()
        {
            var metadataProcessor = new CommerceServerMetadataProcessor();
            var specificEntity = metadataProcessor.GetFullDefinition(null, "SiteContent");
            Assert.IsNotNull(specificEntity, "Entity is null");
            Assert.AreEqual(13, specificEntity.PropertyDefinitions.Count);

            var classDef = metadataProcessor.BuildClassDefinition(null, specificEntity);
            Assert.AreEqual(13, classDef.Properties.Count);
        }

        [TestMethod]
        [Owner("Trying to understand metadata")]
        public void Product()
        {
            var metadataProcessor = new CommerceServerMetadataProcessor();
            var specificEntity = metadataProcessor.GetFullDefinition(null, "Product");
            Assert.IsNotNull(specificEntity, "Entity is null");

            DefinitionDetails(specificEntity);
        }

        [TestMethod]
        [Owner("Trying to understand metadata")]
        public void ProfileEntity()
        {
            var metadataProcessor = new CommerceServerMetadataProcessor();
            var specificEntity = metadataProcessor.GetFullDefinition(null, "UserProfile");
            Assert.IsNotNull(specificEntity, "Entity is null");
            DefinitionDetails(specificEntity);
        }

        [TestMethod]
        [Owner("Trying to understand metadata")]
        public void Sitecontent()
        {
            var metadataProcessor = new CommerceServerMetadataProcessor();
            var specificEntity = metadataProcessor.GetFullDefinition(null, "SiteContent");
            Assert.IsNotNull(specificEntity, "Entity is null");
            DefinitionDetails(specificEntity);
        }

        [TestMethod]
        public void RequestTemplateGeneratorTest()
        {
            var requestTemplateGenerator = new CommerceServerRequestTemplateGenerator();
            var entities = requestTemplateGenerator.GetRequestTemplates();
            Assert.IsNotNull(entities, "Entities null");
        }

        private static void DefinitionDetails(CommerceEntityDefinition specificEntity)
        {
            Console.Out.WriteLine("Property Definitions:");
            foreach (var item in specificEntity.PropertyDefinitions)
            {
                Console.Out.WriteLine(item.Target.Properties["Name"].ToString());
            }

            Console.Out.WriteLine(string.Empty);
            Console.Out.WriteLine(string.Empty);
            Console.Out.WriteLine("Entity Mappings:");
            Console.Out.WriteLine(string.Empty);
            foreach (var item in specificEntity.EntityMappings)
            {
                foreach (var propertyMapping in item.Target.Properties["PropertyMappings"] as StringPairCollection)
                {
                    Console.Out.WriteLine("Key: {0}", propertyMapping.Key);
                    Console.Out.WriteLine("Value: {0}", propertyMapping.Value);
                    Console.Out.WriteLine(string.Empty);
                }

                Console.Out.WriteLine(string.Empty);
                Console.Out.WriteLine(string.Empty);
                Console.Out.WriteLine("Definition Mappings:");
                Console.Out.WriteLine(string.Empty);
                if (item.Target.Properties.Contains("DefinitionMappings"))
                {
                    foreach (var definitionMapping in item.Target.Properties["DefinitionMappings"] as CommerceRelationshipList)
                    {
                        Console.Out.WriteLine("Definition Name: {0} ", definitionMapping.Target.Properties["DefinitionName"].ToString());
                        Console.Out.WriteLine("Commerce Server Definition: {0}", definitionMapping.Target.Properties["CommerceServerDefinitionName"].ToString());
                        Console.Out.WriteLine("Property Mappings");
                        var propertyMappings = definitionMapping.Target.Properties["PropertyMappings"] as StringPairCollection;
                        foreach (var mapping in propertyMappings)
                        {
                            Console.Out.WriteLine("\tKey: {0}", mapping.Key);
                            Console.Out.WriteLine("\tValue: {0}", mapping.Value);
                            Console.Out.WriteLine(string.Empty);
                        }
                    }
                }
            }
        }
    }
}
